﻿using Microsoft.AspNetCore.Mvc;
namespace WebApplication2.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class OrderManageWebApiController : ControllerBase
    {
      public static  OrderService WebApiService = new OrderService() ;


        [HttpGet]
        public string GetOrder(int ono)
        {

            string result = "";
            List<Order> sel = new List<Order>();
            var query1 = from Sel1 in WebApiService.orderData
                         where Sel1.Ono == ono
                         orderby Sel1.Oamount
                         select Sel1;
            sel = query1.ToList();
            for (int i = 0; i < sel.Count; i++)
            {
                result += sel[i].ToString() + "\n";
                for (int m = 0; m < sel[i].orders.Count; m++)
                {
                    result += sel[i].orders[m].ToString();
                }

            }
            return result;
        }

        [HttpPost]
      public bool PostOrder(int select, int Ono, string GName, string CusName, int Oid, string GName1, int GNum, double price)
        {
            
                Order order = new Order(Ono, GName, CusName);
            bool result = false;
                bool equal = false;
                bool equal1 = false;
            if (select == 1)
            {
                foreach (Order i in WebApiService.orderData)
                {
                    if (i.Equals(order))
                    {
                        equal = true;
                    }
                }
                if (!equal)
                {

                    OrderDetail i = new OrderDetail(Oid, GName1, GNum, price);
                    foreach (OrderDetail o in order.orders)
                    {
                        if (i.Equals(o))
                        {
                            equal1 = true;
                        }
                    }
                    if (!equal1)
                    {
                        order.orders.Add(i);
                        order.theWholePrice();
                        WebApiService.orderData.Add(order);
                        result = true;

                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            else {   
                        int ono1 = Ono;
                    bool istrue = false;
                        WebApiService.orderData.ForEach(i =>
                        {
                            if (i.Ono == ono1)
                            {
                                i.AddDetails(Oid, GName1, GNum, price);
                                i.theWholePrice();
                                result = true;
                            }
                        });

                }
            return result;

        }
        [HttpDelete]
        public bool DeleteOrder(int select,int id)
        {
            bool result = false;
            if (select == 1)
            {
                for (int i = WebApiService.orderData.Count - 1; i >= 0; i--)
                {
                    if (WebApiService.orderData[i].Ono == id)
                    {
                        WebApiService.orderData.Remove(WebApiService.orderData[i]);
                        result = true;
                    }
                    
                }
            }
            else { 
                        for (int i = WebApiService.orderData.Count - 1; i >= 0; i--)
                        {
                            if (WebApiService.orderData[i].Ono == id)
                            {
                                WebApiService.orderData[i].DeleteDatails(id);
                        result = true;
                            }
                        }
                        
                }
            return result;

            }

        
        [HttpPut]
        public bool ChangeOrder(int ono,int oid,string GName1,string CusName1,int num,double price)
        {
            bool result = false;
                WebApiService.orderData.ForEach(i =>
                {
                    if (i.Ono == ono)
                    {
                        i.Gname = GName1;
                        i.Customer = CusName1;
                        i.orders.ForEach(m =>
                        {
                            if (m.OId == oid)
                            {
                                m.Goodname = GName1;
                                m.GoodNum = num;
                                m.UnitPrice = price;
                            }
                        }
                        );
                        i.theWholePrice();
                        result = true;
                    }
                }
                            );
               return result;
            }
        }

    }

